#! /bin/bash

script_dir=$(cd "$(dirname "$0")"; pwd)

build_type=$1
if [ -z "$build_type" ]; then
    build_type="Release"
fi

main_ini=$script_dir/config/main.ini

ini_tools=${script_dir}/script/linux/ini_tools.sh
clone_fuction=${script_dir}/script/linux/clone.sh
source ${ini_tools}
source ${clone_fuction}

cmake_bin=$(parse_ini $main_ini environment cmake_bin)
export INTEROP_ROOT=$(parse_ini $main_ini environment INTEROP_ROOT)
export ACIS_ROOT=$(parse_ini $main_ini environment ACIS_ROOT)
export INTEROP_LIBRARY_ZIP_DIR=$(parse_ini $main_ini environment INTEROP_LIBRARY_ZIP_DIR)
export DTK_SDK_DIR=$(parse_ini $main_ini environment DTK_SDK_DIR)
export AMCAX_PATH=$(parse_ini $main_ini environment AMCAX_PATH)

if [ -z "$INTEROP_ROOT" ]; then
    echo "The environment variable for INTEROP_ROOT is not set" ;
    exit -1
fi

if [ -z "$ACIS_ROOT" ]; then
    echo "The environment variable for ACIS_ROOT is not set" ;
    exit -1
fi

if [ -z "$INTEROP_LIBRARY_ZIP_DIR" ]; then
    echo "The environment variable for INTEROP_LIBRARY_ZIP_DIR is not set" ;
    exit -1
fi

if [ -z "$DTK_SDK_DIR" ]; then
    echo "The environment variable for DTK_SDK_DIR is not set" ;
    exit -1
fi

if [ -z "$AMCAX_PATH" ]; then
    echo "The environment variable for AMCAX_PATH is not set" ;
    exit -1
fi

if command -v conan >/dev/null 2>&1; then
    echo "Conan is installed."
else
    echo "Conan is not installed."
    exit -1 
fi

if command -v gcc >/dev/null 2>&1 && command -v g++ >/dev/null 2>&1; then
    echo "GCC and G++ are installed."
else
    echo "GCC or G++ is not installed."
    exit -1 
fi

export LD_LIBRARY_PATH=${ACIS_ROOT}/linux_a64/code/bin:${INTEROP_ROOT}/linux_a64/code/bin:${LD_LIBRARY_PATH}

function build_amcax_interOp()
{
    interOp_ini=$script_dir/config/interOp.ini
    export repo_url=$(parse_ini $interOp_ini repo repo_url)
    export third_part_repo_tag=$(parse_ini $interOp_ini repo branch)
    export third_part_name=$(parse_ini $interOp_ini repo localName)
    export re_clone=$(parse_ini $interOp_ini repo re_clone)
    export commit_id=$(parse_ini $interOp_ini repo commit_id)

    clone_and_deploy
    
    if [ -z "$local_repo_dir" ]; then
        echo "clone ${repo_url} failed"
        exit -1
    fi
    cd $local_repo_dir
    bash build.sh $build_type
    cd $script_dir

    export INTEROP_BIN_ROOT=$local_repo_dir/deploy/install/Release
    export INTEROP_BIN=$local_repo_dir/deploy/install/Release/lib
}

function build_amcax_datakit()
{
    datakit_ini=$script_dir/config/datakit.ini
    export repo_url=$(parse_ini $datakit_ini repo repo_url)
    export third_part_repo_tag=$(parse_ini $datakit_ini repo branch)
    export third_part_name=$(parse_ini $datakit_ini repo localName)
    export re_clone=$(parse_ini $datakit_ini repo re_clone)
    export commit_id=$(parse_ini $datakit_ini repo commit_id)

    clone_and_deploy
    
    if [ -z "$local_repo_dir" ]; then
        echo "clone ${repo_url} failed"
        exit -1
    fi
    cd $local_repo_dir
    bash build.sh $build_type
    cd $script_dir

    export DATAKIT_BIN_ROOT=$local_repo_dir/deploy/install/Release
    export DATAKIT_BIN=$local_repo_dir/deploy/install/Release/lib
}

build_amcax_interOp
build_amcax_datakit

cd $script_dir/..
bash build.sh $build_type
cd $script_dir